<?php

class SmtpMail {

  /**
   * Set the name of the SMTP relay mail server to use to send an email.
   * @param String $value The name of the SMTP mail server.
   * @return String Return TRUE on succes or FALSE on failure.
   * @link http://be.php.net/manual/en/function.is-string.php
   * @link http://be.php.net/manual/en/function.empty.php
   * @link http://be.php.net/manual/en/function.ini-set.php<br />
   * @throws BeObjectRuntimeException
   */
  public static function SmtpServer(IString $value = null) {
    $return = String::EMPTY_STRING;

    if ($value == null) {
      $return = new String(ini_get("SMTP"));
    } else {
      $return = ini_set("SMTP", $value->toString());
    }

    if ($return === false) {
      throw new BeObjectRuntimeException("The SMTP property can not be modified.");
    } else {
      $return = new String($return);
    }
    return $return;
  }

  /**
   * Send a mail message using the specified destination parameters.
   * @param MailMessage $mailMessage The MailMessage to send.
   * @return Boolean Returns true on succes or false on failure.
   * @link http://be.php.net/manual/en/function.empty.php
   * @link http://be.php.net/manual/en/function.mail.php
   * @todo Implement more than one attachment.
   */
  public static function send(MailMessage $mailMessage) {
    $headers = String::EMPTY_STRING;
    $body = String::EMPTY_STRING;

    if (!is_null($mailMessage->getAttachments()) > 0)
      $headers .= "MIME-Version: 1.0\r\n" .
              "Content-Type: multipart/mixed; " .
              "boundary=\"" . $mailMessage->Attachment->getHash() . "\"\r\n";
    elseif ($mailMessage->isHtml())
      $headers .= "MIME-Version: 1.0\r\n" .
              "Content-type: text/html; charset=iso-8859-1\r\n";

    if ($mailMessage->getSenderName() != "" &&
            $mailMessage->getSender() != "")
      $headers .= "From: \"" . $mailMessage->getSenderName() . "\" <" .
              $mailMessage->getSender() . ">\r\n";
    else
    if ($mailMessage->getSender() != "")
      $headers .= "From: <" . $mailMessage->getSender() . ">\r\n";

    if ($mailMessage->getReply() != "")
      $headers .= "Reply-To: <" . $mailMessage->getReply() . ">\r\n";

    if ($mailMessage->getCopy() != "")
      $headers .= "CC: <" . $mailMessage->getCopy() . ">\r\n";

    if ($mailMessage->getHiddenCopy() != "")
      $headers .= "BCC: <" . $mailMessage->getHiddenCopy() . ">\r\n";

    if (!is_null($mailMessage->getAttachments()) > 0) {
      $attachment = "\n\n" . "--" . $mailMessage->Attachment->getHash() . "\n" .
              "Content-Type: application; name=\"" . $mailMessage->Attachment->getFilename() . "\"\r\n" .
              "Content-Transfer-Encoding: base64\r\n" .
              "Content-Disposition: attachment; " .
              "filename=\"" . $mailMessage->Attachment->getFilename() . "\"\r\n\n"
              . $mailMessage->Attachment->getFileContent() .
              "--" . $mailMessage->Attachment->getHash() . "--";

      $body = "--" . $mailMessage->Attachment->getHash() . "\n" .
              "Content-Type: text/html; charset=ISO-8859-1\r\n\n" .
              $mailMessage->getBody() . $attachment;
    }
    else
      $body = $mailMessage->getBody();

    return mail($mailMessage->getRecipient(), $mailMessage->getSubject(), $body, $headers);
  }

}

?>
